/*
JuffEd - A simple text editor
Copyright 2007 Murzin Mikhail

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License 
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include <QCoreApplication>
#include "main.h"
#include "JuffEd.h"
#include <QApplication>
#include <QtGui>
#include <QtDebug>
#include <QDebug>

/* on main Q_INIT_RESOURCE(osApplication);  same name osApplication.qrc  */
/* to access info.plist live write append .... each extension wo app open go in ...*/

#define osApplication \
    (static_cast<OS_application*>(QCoreApplication::instance()))
    
class OS_application : public QApplication
{
    Q_OBJECT
//
public:
 OS_application( int &argc, char **argv )
  : QApplication(argc, argv)

{
        /* to enable QT Setting QSetting setup save x11 session language and other  */
        setOrganizationName(_ORGANIZATION_NAME_);
        setOrganizationDomain(_PROGRAM_NAME_DOMAINE_);
        setApplicationName(_PROGRAM_NAME_);
        bool firstime = false;
        /* debug fase commen out to rebuild file ... */
        //////////qt_unlink(APPLICATION_SETTING);
    
        if (!is_file(APPLICATION_SETTING)) {
            fwriteutf8(APPLICATION_SETTING,fopenutf8(":/resource/juffed.conf"));
            fwriteutf8(WORDLIST_FILE,fopenutf8(":/resource/QTclassName.txt"));
            CopyResource(QString::fromUtf8(":/resource.qrc"));
            /* first install */
            firstime = true;
        }
        /* translate here */
        Mainwin = new JuffEd();
        Mainwin->setWindowTitle(_PROGRAM_TITLE_);
        
           QRect screenSize = desktop()->availableGeometry();
            Mainwin->setMinimumSize ( screenSize.width() / 2 , screenSize.height() / 2 );
            if (firstime) {
            //////Mainwin->setMinimumSize ( QSize ( screenSize.width() / 2 , screenSize.height() / 2 )  );
            Mainwin->move (15,15);
            } else {
            /* or restore last x session qsetting */
            Mainwin->showMaximized();
            }
        
        
        
	    Mainwin->show();
        ////////qDebug() << "QCoreApplication start  session id " << sessionId();
        ////////qDebug() << "QCoreApplication arguments ()  " << arguments();
        /* small x11 window manager or console run */
        QStringList files = arguments();
         if (files.size() > 1) {

           for (int i = 0; i < files.size(); ++i) {  
              if (i > 0) {           
              QString onef = files.at(i);
              if (is_file(onef)) {
                  Mainwin->OpenFile(onef);
              }
            }
           }

          }
          
           
          
          
            
    
    
       
}
protected:
bool event(QEvent *ev)
{
    bool eaten;
    switch (ev->type()) {
    case QEvent::FileOpen:
        /* on or more from drag event to icon mac linux win */
        osfile = static_cast<QFileOpenEvent *>(ev)->file();
        //////qDebug() << "Qcore file " << osfile;
        Mainwin->OpenFile(osfile);
        eaten = true;
        break;
    case QEvent::Close: {
        /////////QCloseEvent *closeEvent = static_cast<QCloseEvent *>(ev);
        Mainwin->SaveXsession();
        eaten = true;
        break;
    }
    default:
        eaten = QApplication::event(ev);
        break;
    }
    return eaten;
}
private:
    QString osfile;
    JuffEd *Mainwin;
void CopyResource( QString filename )
{
    QFile xmlfile(filename);
    if(!xmlfile.open( QIODevice::ReadOnly ) ) {
    return;
    }
    QString errorStr, obname, inhalt;
    int errorLine;
    int errorColumn;
    QDomDocument doc; 
     if (!doc.setContent(&xmlfile,false, &errorStr, &errorLine, &errorColumn)) {
     return;
     xmlfile.close();
     } 
    QDomElement root = doc.documentElement();
    if( root.tagName() != "RCC" ) {
    return;
    }
    QDomElement child = root.firstChildElement("qresource");
        while (!child.isNull()) {
            
           QDomAttr a1 = child.attributeNode("prefix");
           QDomElement rcfile = child.firstChildElement("file");
            while (!rcfile.isNull()) {
                   QString name = rcfile.text().trimmed();
            const QString rin = QString(":%2/%1").arg(name).arg(a1.value());
            const QString dest = APPCACHEDIR + name;
                if (!is_file(dest)) {
                   bool gos = Copy_To(rin,dest);
                    /////////qDebug() << "rfile->" << gos << "|" << dest;
                }             
            rcfile = rcfile.nextSiblingElement("file");
            }
           child = child.nextSiblingElement("qresource");
        } 
    xmlfile.close();
}


};


int main(int argc, char* argv[]) {
    Q_INIT_RESOURCE(resource);
	OS_application a(argc, argv);
    a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
	return a.exec();
}



#include "main.moc"







